home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1644 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.4 KB  |  105 lines

  1. Newsgroups: comp.lang.c,comp.lang.c++
  2. Path: ncrgw2.ncr.com!ncrhub6!daynews!falcon!news
  3. From: Dick Menninger <Dick.Menninger@DaytonOH.ATTGIS.COM>
  4. Subject: Re: RANDOM NUMBER GENERATOR
  5. X-Nntp-Posting-Host: 149.25.99.44
  6. Message-ID: <DL1Hyq.I5u@falcon.daytonoh.attgis.com>
  7. Sender: news@falcon.daytonoh.attgis.com (News administrative Login)
  8. Reply-To: Dick.Menninger@DaytonOH.ATTGIS.COM (mennid)
  9. Organization: AT&T Global Information Solutions
  10. X-Newsreader: DiscussIT 2.5.1.3 for MS Windows [AT&T Software Products Division]
  11. References: <4cniff$11o$1@mhade.production.compuserve.com>
  12. Date: Thu, 11 Jan 1996 23:16:02 GMT
  13.  
  14.  
  15. > ==========Merlin Chowkwanyun, 1/6/96==========
  16.  
  17. > Just thought you might like to know that I found this random
  18. > number generator
  19. > lying around in an old C++ magazine
  20.  
  21. > /declares a global random number generating function
  22.  
  23. > int random(int MaxVal)
  24. > {
  25. >  return rand() % MaxVal;
  26. > }
  27.  
  28. > Be sure to include the header file stdlib.h
  29.  
  30. > Merlin
  31.  
  32. You can use this as long as you don't mind rather mediocre
  33. results.  There are several things wrong with most of the
  34. discussion in this thread.  The basic wrongs are: using
  35. the same mill for each supposedly independent random
  36. variable; using a composite number [as opposed to a
  37. prime] to mod the mill value.  Using the same mill for all
  38. makes them dependent more than you suspect is likely.
  39. Those games that seem to have patterns of behavior
  40. that occur too frequently often do because of this.
  41. So, get Knuth's Seminumerical Algorithms to learn the
  42. rules for mills.  Mills are half of the problem of a random
  43. variable.  Mapping the mill into a small integer range is the
  44. other half [ignoring floating point, which can reduced to
  45. several integer parts for best results].  This part is easy
  46. to state, but a bitch to prove [partition theory, ...].
  47. Convert your small integer range [lv, hv] to [0, hv-lv].
  48. Get a prime, p, larger than hv-lv.  Use it to mod the mill
  49. value and throw away values larger than hv-lv (i.e.,
  50. recrank the mill).  This produces the best pseudo-random
  51. variable possible with a mill of range X (X usually 2**32
  52. but can now readily be 2**64, a major boon to randomness).
  53. You can even figure out the length of sequence that can
  54. be fully represented [p1, p2, ..., pN] by successive sampling,
  55. and this is major measure of the effective quality.
  56. Solve the equation p**y = X for y and y is the size of the
  57. sequence [this is a hard part to prove].  Consider a prime
  58. near 256.  Its sequence length is about 4 for 32-bit mills.
  59. to get a sequence length around 8, you need a prime range
  60. near 16 for a 32-bit mill, or a 64-bit mill for a prime range
  61. near 256. [This assumes full-cycle mills: see Knuth]
  62.  
  63. So, to generate really good random variables, you create
  64. separate random variable objects for each random variable.
  65. That means you build a collection of classes to do key parts.
  66. One key part is the ability to create distinct mills. This
  67. is not very hard.  You will need a table of values to use
  68. for the multiplier part [a prime sized table] and separate
  69. mill-based source of odd add parts (the prime size of the
  70. table means that you use every multiplier with every add
  71. before repeating.  Then you use a third mill to create initial
  72. seeds.
  73.  
  74. Picking the primes for mod-ing either requires a table (with
  75. a search) or a prime sieve algorithm.  I have used both.
  76. I even created my own fast sieve for it.
  77.  
  78. All of the above is private stuff for the integer random
  79. variable class and needs only exist in its .cpp file.
  80. Of course, you can do it in C without classes if that is
  81. what you have to work with.  In fact, that is how I first
  82. implemented it in the mid-1970s.  But the idea was made
  83. for C++.
  84.  
  85. Note that separating out your random variables like this
  86. has many noticeable effects.  Consider a game such as
  87. a Rogue variant.  When you think in terms of separate
  88. variables, you now readily control what retry abuses
  89. are available to the player.  Of course, eliminating
  90. one may only create a different one.  But it becomes
  91. a design decision!  In some cases, you may consider pools
  92. of free random vraiables of a given type, IF BOTH:
  93. creation costs are an issue for that type of variable,
  94. and the points at which they would be freed don't
  95. create a bias pattern in their values (proving that
  96. could be interesting ;-) ).  I always generate new ones
  97. and destroy old ones to insure that maximal quality
  98. variables are used (this is theory I am sure of, and
  99. it is easier to do).
  100.  
  101. Good Day
  102. Dick
  103. Dick.Menninger@DaytonOH.ATTGIS.COM
  104.  
  105.